package com.haojiang.mh.ui.activity

import android.content.Context
import android.content.Intent
import android.location.LocationManager
import android.os.Build
import android.provider.Settings
import android.view.View
import com.haojiang.mh.R
import com.haojiang.mh.cache.Const
import com.haojiang.mh.service.ble.BleHandle
import com.haojiang.mh.service.event.OneEvent
import com.haojiang.mh.ui.base.BaseActivity
import com.haojiang.mh.utils.ActManager
import com.haojiang.mh.utils.NetUtil
import com.haojiang.mh.widget.CommonDialog
import kotlinx.android.synthetic.main.activity_common_title.*
import kotlinx.android.synthetic.main.activity_search_wifi_fail.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * WiFi配置失败
 *
 * Created by ZapFive on 2019-05-26.
 *
 * wuzhuang@mirahome.me
 */
class SearchWiFiFailActivity : BaseActivity() {

    private var entrance = -1 //1:WiFi未打开 2:搜索不到WiFi 3:配网失败

    override fun getLayoutId() = R.layout.activity_search_wifi_fail

    override fun getTitleName() = ""

    override fun initActivity() {
        entrance = intent.getIntExtra(Const.EXTRA_DATA, -1)
    }

    override fun initView() {
        aiv_activity_back.visibility = View.GONE
        if (entrance == 2 || entrance == 3) {
            tv_activity_right.visibility = View.VISIBLE
            tv_activity_right.text = getString(R.string.txt_cancel_add)
            tv_activity_right.setOnClickListener {
                if (BleHandle.instance.isConnected()) {
                    BleHandle.instance.disConnectDevice()
                    BleHandle.instance.onDeviceDisconnect(true)
                } else {
                    ActManager.redirectToBottomActivity()
                    context.finish()
                }
            }
        }
        tv_tip_3.setOnClickListener {
            //TODO 跳转WiFi打开提示
        }
        tv_retry.setOnClickListener {
            when (entrance) {
                1 -> if (!NetUtil.getWifiEnabled(context)) showToast(R.string.txt_open_wifi_tip)
                2 -> {
                    //android 6.0以上wifi扫描需要定位权限
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !checkGPSIsOpen()) {
                        CommonDialog.Builder(context, 1)
                                .setContent(getString(R.string.tip_open_location))
                                .setRightClick { _, _ ->
                                    val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                                    startActivityForResult(intent, 0xCCCC)
                                }
                                .create().show()
                    } else {
                        showLoadingDialog()
                        NetUtil.openAndScanWiFi(context)
                    }
                }
                3 -> context.finish()
            }
        }

        when (entrance) {
            1 -> {
                tv_tip_1.visibility = View.INVISIBLE
                tv_tip_2.visibility = View.INVISIBLE
                tv_tip_3.visibility = View.VISIBLE
                aiv_show_img.setImageResource(R.drawable.img_no_wifi)
            }
            2 -> {
                tv_tip_1.visibility = View.VISIBLE
                tv_tip_2.visibility = View.VISIBLE
                tv_tip_3.visibility = View.INVISIBLE
                tv_tip_1.text = getString(R.string.txt_no_wifi_tip_1)
                tv_tip_2.text = getString(R.string.txt_no_wifi_tip_2)
                aiv_show_img.setImageResource(R.drawable.img_no_device)
            }
            3 -> {
                tv_tip_1.visibility = View.VISIBLE
                tv_tip_2.visibility = View.VISIBLE
                tv_tip_3.visibility = View.INVISIBLE
                tv_tip_1.text = getString(R.string.txt_config_wifi_fail)
                tv_tip_2.text = getString(R.string.txt_config_wifi_fail_tip)
                aiv_show_img.setImageResource(R.drawable.img_no_device)
            }
        }
    }

    /**
     * 确定GPS打开
     */
    private fun checkGPSIsOpen(): Boolean {
        val lm = getSystemService(Context.LOCATION_SERVICE) as LocationManager
        return lm.isProviderEnabled(LocationManager.GPS_PROVIDER)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onReceiveEvent(event: OneEvent) {
        when (event.type) {
            Const.DISCONNECT_DEVICE -> {
                ActManager.redirectToBottomActivity()
                context.finish()
            }
            Const.SCAN_WIFI_RESULT -> {
                hideLoadingDialog()
                if (NetUtil.getWifiList(context).isNotEmpty()) {
                    context.finish()
                }
            }
        }
    }

    override fun onBackClick() {
        // do nothing
    }
}